<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="6" :md="12" :sm="24">
            <a-form-item label="红包名称">
              <a-input placeholder="请输入红包名称" v-model="queryParam.name" ></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="6" :md="12" :sm="24">
            <a-form-item label="时间筛选">
              <a-range-picker @change="panelChange" v-model="queryParamRangePicker"/>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="6" :md="12" :sm="24">
            <a-form-item label="状态">
              <a-select default-value="1" placeholder="全部状态" v-model="queryParam.status" >
                <a-select-option :value="item.val" v-for="(item, index) in statusUl" :key="index" >{{ item.name }}</a-select-option >
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="6" :md="12" :sm="24">
            <a-form-item label="发放类型">
              <a-select default-value="1" placeholder="全部类型" v-model="queryParam.sendType" >
                <a-select-option :value="item.val" v-for="(item, index) in sendTypeUl" :key="index" >{{ item.name }}</a-select-option >
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="6" :md="12" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons" >
              <a-button type="primary" @click="searchQuery" icon="search" >查询</a-button >
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px" >重置</a-button >
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="crudCreate()" type="primary" icon="plus">新增</a-button>
    </div>
    <!-- table区域-begin -->
    <div>
      <a-table ref="table" size="middle" bordered rowKey="id" :scroll="{ x: 1300 }" :columns="columns" :dataSource="tableData" :pagination="ipagination" :loading="loading" @change="handleTableChange" >
        <!--操作-->
        <span slot="action" slot-scope="text, record" >
          <a-button type="primary" @click="handleDetail(record)">详情</a-button>
          <!-- <a-button type="primary" @click="crudUpdate(record)">编辑</a-button> -->
          <a-popconfirm title="确认停止发放?" ok-text="确定" cancel-text="取消" @confirm="crudStop(record)">
            <a-button type="primary">停止发放</a-button>
          </a-popconfirm>
          <a-popconfirm title="确认删除?" ok-text="确定" cancel-text="取消" @confirm="crudDelete(record.id)">
            <a-button type="danger" icon="delete"></a-button>
          </a-popconfirm>
        </span>
        <!--图片预览块-->
        <span slot="imgPath" slot-scope="text, record">
          <img :src="record.img" width="100px" height="100px" />
        </span>
      </a-table>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <coupon-list-modal ref="updateForm" @closeModal="closeModal"></coupon-list-modal>
  </a-card>
</template>

<script>
import moment from 'moment'
import couponListModal from './modules/couponListModal'
import ApiTree from "@/api/market/couponList";
import { Slime, crud } from "@/mixins/slime";

export default {
  name: "couponList",
  mixins: [Slime],
  components: {
    couponListModal
  },
  data() {
    return {
      statusUl: [
        { name: "关闭", val: 0 },
        { name: "启用", val: 1 },
      ],
      sendTypeUl:[
        { name: "普通", val: 0 },
        { name: "注册赠送", val: 1 },
        { name: "首次收藏商家赠送", val: 2 },
      ],
      tableData: [],
      columns: [
        {
          title: "红包名称",
          align: "center",
          dataIndex: "name",
          width: 120,
        },
        {
          title: "数量",
          align: "center",
          dataIndex: "total",
          customRender: function(total) {
            if(total == 0){
              return '无限'
            }else{
              return total
            }
          }
        },
        {
          title: "单用户限领数量",
          align: "center",
          dataIndex: "limitCount",
          width: 80,
        },
        {
          title: "满金额",
          align: "center",
          dataIndex: "fullMoney",
          customRender: function(money) {
            return (parseInt(money) / 100).toFixed(2);
          }
        },
        {
          title: "减金额",
          align: "center",
          dataIndex: "minusMoney",
          customRender: function(money) {
            if(money){
              return (parseInt(money) / 100).toFixed(2);
            }else{
              return ''
            }
          }
        },
        {
          title: "折扣",
          align: "center",
          dataIndex: "discount",
          customRender: function(money) {
            if(money){
              return (parseInt(money) / 10).toFixed(1);
            }else{
              return ''
            }
          }
        },
        {
          title: "有效天数",
          align: "center",
          dataIndex: "days",
          customRender: function(days) {
            if(days){
              return days;
            }else{
              return ''
            }
          }
        },
        {
          title: "使用券开始时间",
          align: "center",
          dataIndex: "startTime",
          width: 100,
        },
        {
          title: "使用券截至时间",
          align: "center",
          dataIndex: "endTime",
          width: 100,
        },
        {
          title: "适用类型",
          align: "center",
          dataIndex: "applyType",
          customRender: function (status) {
            switch (status) {
              case 1:
                return "门店通用 ";
              case 2:
                return "指定分类";
              case 3:
                return "指定商品";
              default:
                return "未知状态";
            }
          },
        },
        {
          title: "发放类型",
          align: "center",
          dataIndex: "sendType",
          customRender: function (status) {
            switch (status) {
              case 0:
                return "普通 ";
              case 1:
                return "注册赠送";
              case 2:
                return "首次收藏商家赠送";
              default:
                return "未知状态";
            }
          },
        },
        {
          title: "时间",
          align: "center",
          dataIndex: "createTime",
          width: 100,
        },
        {
          title: "状态",
          align: "center",
          dataIndex: "status",
          customRender: function (status) {
            switch (status) {
              case 0:
                return "关闭 ";
              case 1:
                return "启用";
              default:
                return "未知状态";
            }
          },
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' },
        },
      ],
    };
  },
  created() {
    crud(ApiTree);
    this.loadData();
  },
  methods: {
    // 加载表格数据
    loadData(arg) {
      this.queryParam = {...this.queryParam, merchantId:0}
      this.loadDataPack(arg).then((res) => {
        let tableData = [];
        if (res.result) {
          tableData = res.result.records;
        }
        this.tableData = tableData;
      });
    },
    //停止发放
    crudStop(record){
      const that = this;
      let param = {
        ...record,
        status: 0,
      }
      ApiTree.getUpdate(param)
        .then((res) => {
          that.$message.success('停止成功');
        })
        .finally(() => {
          that.loadData()
        });
    },
    //改变筛选时间
    panelChange(date){
      let startTime = moment(date[0]).format('YYYY-MM-DD');
      let endTime = moment(date[1]).format('YYYY-MM-DD');
      this.queryParam = {...this.queryParam, startCreateDate:startTime, endCreateDate:endTime}
    },
    //详情
    handleDetail(record){
      this.$refs.updateForm.crudUpdate(record);
    },
  },
};
</script>
<style scoped>
</style>